package ui 
{
	import data.*;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import org.flixel.FlxG;
	
	import ui.textfield.GCStylizedText;
	import ui.textfield.GCTextField;
	/**
	 * ...
	 * @author Jason Won
	 */
	public class CitySprite extends Sprite
	{
		private var _cityId:int;
		private var _selectableArea:Sprite;
		private var _nameDisplay:GCStylizedText;
		private var _restrictedTravelDisplay:GCTextField;
		public var _toggle:Boolean;
		
		/**
		 * Constructor
		 * @param	name
		 */
		public function CitySprite(name:int) 
		{
			_cityId = name;
			_selectableArea = new Sprite;
			_nameDisplay = new GCStylizedText();
			_restrictedTravelDisplay = new GCTextField();
			_nameDisplay.text = GCCityData.getNameFromId(name);
			_toggle = true;
			
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseEnter);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseExit);
			
			init();			
		}
		
		/**
		 * Function to disable the city 
		 */
		public function disableCity():void
		{
			this.alpha = 0.5;
			_toggle = false;
		}
		
		public function enableCity():void
		{
			this.alpha = 1;
			_toggle = true;
		}
		
		public function highlightCity(doHighlight:Boolean):void
		{
			if (doHighlight)
			{
				this.graphics.lineStyle(1);
				this.graphics.beginFill(GCConstants.COLOR_RED, 0.7);
				this.graphics.drawCircle(this.width / 4, this.width / 4, this.width / 3);
				this.graphics.endFill();
			}
			else
			{
				this.graphics.clear();
			}
		}
		
		private function init():void
		{			
			var bmd:BitmapData = FlxG.addBitmap(GCResources.cityImage);
			var myBitmap:Bitmap = new Bitmap(bmd);
			_selectableArea.addChild(myBitmap);
			
			var coord:Point = GCCityData.getCoordinatesFromId(_cityId);
			setXY(coord.x, coord.y);
			
			_nameDisplay.y = -20;
			_nameDisplay.visible = false;
			_restrictedTravelDisplay.text = "You cannot travel here now."
			_restrictedTravelDisplay.visible = false;
			
			this.addChild(_nameDisplay);
			this.addChild(_restrictedTravelDisplay);
			this.addChild(_selectableArea);
			
			_nameDisplay.x = 20;
			_nameDisplay.height = this.height;
			_restrictedTravelDisplay.x = _nameDisplay.x;
			_restrictedTravelDisplay.y = _nameDisplay.y + 20;//_nameDisplay.height;
			_restrictedTravelDisplay.height = this.height / 3;
		}
		
		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------		
		
		private function onMouseEnter(event:MouseEvent):void
		{
			_nameDisplay.visible = true;
			
			if (!_toggle)
				_restrictedTravelDisplay.visible = true;
		}
		
		private function onMouseExit(event:MouseEvent):void
		{
			_nameDisplay.visible = false;
			_restrictedTravelDisplay.visible = false;
		}
		
		//----------------------------------------------------------------------
		//	GET FUNCTIONS
		//----------------------------------------------------------------------		
		
		// Returns the name of the city
		public function getCityId():int
		{
			return _cityId;
		}
				
		//----------------------------------------------------------------------
		// SET FUNCTIONS
		//----------------------------------------------------------------------		

		// Sets the coordinate of the city
		public function setXY(x:int, y:int):void
		{
			this.x = x;
			this.y = y;
		}
	}
}